<template>
	<view>
		<view class="tabr" :style="{top:headerTop}">
			<view :class="navid == item.id?'on':''" v-for="(item,index) in navlist" :key="index"
				@click="changeNav(item)">
				{{item.name}}
			</view>
		</view>
		<view class="place">

		</view>
		<view class="list">
			<!-- 优惠券列表 -->
			<view class="sub-list valid" :class="subState">
				<view class="tis" v-if="couponValidList.length==0">没有数据~</view>
				<view class="row" v-for="(row,index) in couponValidList" :key="index">
					<!-- 删除按钮 -->
					<view class="menu" @tap.stop="deleteCoupon(row.id,couponValidList)">
						<view class="icon shanchu"></view>
					</view>
					<!-- content -->
					<view class="carrier"
						:class="[typeClass=='valid'?theIndex==index?'open':oldIndex==index?'close':'':'']" @t>

						<view class="top">
							<view class="left">
								<view class="num">
									￥<text class="jian">{{row.jian}}</text>
								</view>
								<text>满{{row.man}}元立减{{row.jian}}元</text>
							</view>
							<view class="right" @click="toBuy(row)">
								{{row.type_name}}
							</view>
						</view>
						<view class="term">
							<text>开始时间：{{row.start_time}}</text>
							<text>结束时间：{{row.end_time}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		MyQuan
	} from "@/api/user.js"
	export default {
		data() {
			return {
				couponValidList: [],
				headerTop: 0,
				//控制滑动效果
				typeClass: 'valid',
				subState: '',
				theIndex: null,
				oldIndex: null,
				isStop: false,
				page: 1,
				goods_id: '',
				has_list: false,
				edit: '',
				last_page: 1,
				navlist: [{
						name: '全部',
						id: 5
					},
					{
						name: '待使用',
						id: 1
					},
					{
						name: '已转赠',
						id: 2
					},
					{
						name: '已使用 ',
						id: 3
					},
					{
						name: '已过期',
						id: 4
					}
				],
				navid: 5
			}
		},
		onReachBottom() {
			if (this.page < this.last_page) {
				this.page++
				this.MyQuan()
			}
		},
		onLoad(e) {
			if (e.goods_id) {
				this.goods_id = e.goods_id
			}
			//兼容H5下排序栏位置
			// #ifdef H5
			//定时器方式循环获取高度为止，这么写的原因是onLoad中head未必已经渲染出来。
			let Timer = setInterval(() => {
				let uniHead = document.getElementsByTagName('uni-page-head');
				if (uniHead.length > 0) {
					this.headerTop = uniHead[0].offsetHeight + 'px';
					clearInterval(Timer); //清除定时器
				}
			}, 1);
			// #endif
			this.MyQuan()
		},
		methods: {
			changeNav(item) {
				this.navid = item.id
				this.page = 1
				this.last_page = 1
				this.couponValidList = []
				this.MyQuan()
			},
			toBuy(row) {
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.quanInfo = row; //修改上一页data里面的tagIndex 参数值
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});

			},
			MyQuan() {
				MyQuan({
					status: this.navid,
					page: this.page,
				}).then(res => {
					if (res.code == 0) {
						this.couponValidList = this.couponValidList.concat(res.data.data)
						this.last_page = res.data.last_page
					}
				})
			},
			switchType(type, i) {
				this.type = i
				this.page = 1
				this.has_list = false
				this.couponValidList = []
				if (type == 1) {
					this.couponValidList = []
				} else {
					this.couponinvalidList = []
				}
				this.QuanList()
				if (this.typeClass == type) {
					return;
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				})
				this.typeClass = type;
				this.subState = this.typeClass == '' ? '' : 'show' + type;
				setTimeout(() => {
					this.oldIndex = null;
					this.theIndex = null;
					this.subState = this.typeClass == 'valid' ? '' : this.subState;
				}, 200)
			},
			//控制左滑删除效果-begin
			touchStart(index, event) {
				//多点触控不触发
				if (event.touches.length > 1) {
					this.isStop = true;
					return;
				}
				this.oldIndex = this.theIndex;
				this.theIndex = null;
				//初始坐标
				this.initXY = [event.touches[0].pageX, event.touches[0].pageY];
			},
			touchMove(index, event) {
				//多点触控不触发
				if (event.touches.length > 1) {
					this.isStop = true;
					return;
				}
				let moveX = event.touches[0].pageX - this.initXY[0];
				let moveY = event.touches[0].pageY - this.initXY[1];

				if (this.isStop || Math.abs(moveX) < 5) {
					return;
				}
				if (Math.abs(moveY) > Math.abs(moveX)) {
					// 竖向滑动-不触发左滑效果
					this.isStop = true;
					return;
				}

				if (moveX < 0) {
					this.theIndex = index;
					this.isStop = true;
				} else if (moveX > 0) {
					if (this.theIndex != null && this.oldIndex == this.theIndex) {
						this.oldIndex = index;
						this.theIndex = null;
						this.isStop = true;
						setTimeout(() => {
							this.oldIndex = null;
						}, 150)
					}
				}
			},

			touchEnd(index, $event) {
				//解除禁止触发状态
				this.isStop = false;
			},

			//删除商品
			deleteCoupon(id, List) {
				let len = List.length;
				for (let i = 0; i < len; i++) {
					if (id == List[i].id) {
						List.splice(i, 1);
						break;
					}
				}
				this.oldIndex = null;
				this.theIndex = null;
			},

			discard() {
				//丢弃
			}


		}
	}
</script>
<style lang="scss">
	view {
		display: flex;
		flex-wrap: wrap;

	}

	page {
		position: relative;
	}

	.hidden {
		display: none !important;
	}

	.place {
		width: 100%;
		height: 95upx;
	}

	.tabr {
		background-color: #fff;
		width: 94%;
		height: 95upx;
		padding: 0 3%;
		border-bottom: solid 1upx #dedede;
		position: fixed;
		top: 0;
		z-index: 10;

		view {
			width: 20%;
			height: 90upx;
			justify-content: center;
			align-items: center;
			font-size: 14px;
			color: #999;
		}

		.on {
			color: #D83428;
			border-bottom: 2px solid #D83428;
		}

		.border {
			height: 4upx;
			background-color: #D83428;
			transition: all .3s ease-out;

			&.invalid {
				transform: translate3d(100%, 0, 0);
			}
		}

	}

	.list {
		width: 100%;
		display: block;
		position: relative;
	}

	@keyframes showValid {
		0% {
			transform: translateX(-100%);
		}

		100% {
			transform: translateX(0);
		}
	}

	@keyframes showInvalid {
		0% {
			transform: translateX(0);
		}

		100% {
			transform: translateX(-100%);
		}
	}

	.sub-list {
		&.invalid {
			position: absolute;
			top: 0;
			left: 100%;
			display: none;
		}

		&.showvalid {
			display: flex;
			animation: showValid 0.20s linear both;
		}

		&.showinvalid {
			display: flex;
			animation: showInvalid 0.20s linear both;
		}

		width: 100%;
		padding: 20upx 0 120upx 0;

		.tis {
			width: 100%;
			height: 60upx;
			justify-content: center;
			align-items: center;
			font-size: 32upx;
		}

		.row {
			width: 92%;
			height: 32vw;
			margin: 20upx auto 10upx auto;
			border-radius: 8upx;
			// box-shadow: 0upx 0 10upx rgba(0,0,0,0.1);
			align-items: center;
			position: relative;
			overflow: hidden;
			z-index: 4;
			border: 0;
			border: 1px solid #E1E1E1;
			border-radius: 10px;
			padding-bottom: 5px;

			.menu {
				.icon {
					color: #fff;
					font-size: 50upx;
				}

				position: absolute;
				width: 28%;
				height: 100%;
				right: 0;
				justify-content: center;
				align-items: center;
				background-color: red;
				color: #fff;
				z-index: 2;
				border-radius: 10px;
			}

			.carrier {
				@keyframes showMenu {
					0% {
						transform: translateX(0);
					}

					100% {
						transform: translateX(-28%);
					}
				}

				@keyframes closeMenu {
					0% {
						transform: translateX(-28%);
					}

					100% {
						transform: translateX(0);
					}
				}

				&.open {
					animation: showMenu 0.25s linear both;
				}

				&.close {
					animation: closeMenu 0.15s linear both;
				}

				background-color: #fff;
				position: absolute;
				width: 100%;
				padding: 0 0;
				height: 100%;
				z-index: 3;
				flex-wrap: nowrap;
				border-radius: 10px;
				overflow: hidden;
				flex-direction: column;

				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					background: #FDE9E8;
					width: 100%;
					flex-wrap: unset;
					height: 55%;
					border-radius: 10px;

					.left {
						display: flex;
						align-items: center;
						flex-wrap: unset;
						font-size: 12px;
						flex: 1;
						padding-left: 10px;
						border-right: 1px dashed #ccc;
						font-weight: bold;
						height: 70%;
						white-space: nowrap;
					}

					.num {
						color: #FE1413;
						font-size: 18px;
						font-weight: bold;
						align-items: flex-end;
						margin-right: 10px;
						flex-wrap: unset;

						.jian {
							font-size: 22px;
						}
					}

					.right {
						color: #FE1413;
						text-align: center;
						font-size: 14px;
						width: 80px;
						text-align: center;
						justify-content: center;
					}
				}

				.term {
					width: 90%;
					margin: 0 10px;
					font-size: 26upx;
					color: #6B6B6B;
					display: flex;
					align-items: center;
					justify-content: space-between;

					text {
						margin-top: 20px;
					}
				}
			}
		}



	}

	.invalid {
		.top {
			background: #F1F1F1 !important;

			.num,
			.right {
				color: #9C9C9c !important;
			}
		}
	}
</style>